package easy;

public class Solution_1624 {
    public int maxLengthBetweenEqualCharacters(String s) {
        int[][] array = new int[26][];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int index = c - 'a';
            if (array[index] == null) {
                array[index] = new int[]{i, i};
            } else {
                array[index][0] = Math.min(array[index][0], i);
                array[index][1] = Math.max(array[index][1], i);
            }
        }
        int res = -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null && array[i][0] != array[i][1]) {
                res = Math.max(array[i][1] - array[i][0] - 1, res);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution_1624 model = new Solution_1624();
        System.out.println(model.maxLengthBetweenEqualCharacters("mgntdygtxrvxjnwksqhxuxtrv"));
    }
}
